package com.maggroup.conference_system.service.impl;

import com.maggroup.conference_system.common.InfoPage;
import com.maggroup.conference_system.common.PageInfo;
import com.maggroup.conference_system.common.utils.DynamicSpecifications;
import com.maggroup.conference_system.entity.Question;
import com.maggroup.conference_system.repositories.QuestionRepository;
import com.maggroup.conference_system.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Optional;

@Service
public class QuestionServiceImpl implements QuestionService {

	@Autowired
	private QuestionRepository questionRepository;

	@Override
	public void ignore(Question question) {
		Optional<Question> result = questionRepository.findById(question.getId());
		if(result.isPresent()){
			Boolean ignore = result.get().getOverlook();
			result.get().setOverlook(!ignore);
			questionRepository.saveAndFlush(result.get());
		}
	}

	@Override
	public Question add(Question question) {
		return questionRepository.save(question);
	}

	@Override
	public InfoPage<Question> getQuestionPage(Map<String, Object> map, PageInfo pageInfo) {
		Page<Question> pageQuestions = questionRepository.findAll(DynamicSpecifications.createSpecification(map),
				pageInfo);
		return new InfoPage<Question>(pageQuestions.getContent(), pageQuestions.getTotalElements());
	}

}
